$def with (user, loans)

$var title: $_("Books You've Checked Out")

<style type="text/css">
.date, .waitrank {
    margin: 5px 0px;
}
.dialog .title {
    font-weight: bold;
}

tr.expired {
    background: #ffcdcd;
}

td.status, th.status {
    padding: 15px;
    text-align: center;
}

tr.row-available .status-value {
    color: darkgreen;
}

tr.row-available {
    background: #fffdcd;
}

td.actions form + form {
    margin-top: 5px;
}

th.action {
    width: 150px;
}
th.status {
    width: 150px;
}
</style>

$if len(loans) >= 5:
    <script type="text/javascript">
    <!--
    window.q.push(function(){
        \$("#contentMsg").show();
        \$(".alert span").css({'font-size':'.6875em','color':'#333'})
        \$(".alert span").text("$_("You have 5 ebooks out at the moment. That's the limit! You can free up slots by returning books early.")");
    });
    //-->
    </script>

<script type="text/javascript">
<!--
window.q.push(function(){
    \$(".local-date-time").each(function() {
        var expiry_utc = \$(this).attr("data-expiry-utc");
        var options = {hour: '2-digit', minute:'2-digit', month:'2-digit', day:'2-digit', year:'numeric'};
        \$(this).html((new Date(expiry_utc)).toLocaleString([], options));
    });
});
//-->
</script>
<div id="contentHead">
  <div class="small sansserif grey" style="margin-bottom:5px;"><a href="$ctx.user.key">$ctx.user.displayname</a> /</div>
</div>
<div id="contentBody">
  <h1>$_("Books You've Checked Out")</h1>
  $if len(loans) == 0:
    <div><em>$_("You've not checked out any books at this moment.")</em></div>
$else:
  $# Have current loans
  <div id="borrowTable" class="borrow collapse">
    <table>
        <thead>
            <tr>
                <th class="titles" colspan="2">
                $if len(loans) == 1:
                    $_("1 Current Loan")
                $else:
                    $_("%(count)d Current Loans", count=len(loans))
                </th>
                <th class="expires">$_("Loan Expires")</th>
                <th class="actions">$_("Loan actions")</th>
            </tr>
        </thead>
        <tbody>
        <!-- for each borrow -->
        $for loan in loans:
          $ book = get_document(loan['book'])
          $if book:
              <tr data-userid="$(loan['userid'])">
                <td class="cover">
                $:render_template('covers/book_cover_small', book)
                </td>
                <td class="titles">
                    <span class="book">
                    <a href="$book.key" class="borrowResults">
                    <strong>$book.title</strong>
                    </a>
                    </span>
                    <span class="author">
                    $:macros.AuthorList(book.get_authors())
                    </span>
                    <div class="date">
                    Borrowed $datestr(datetime_from_utc_timestamp(loan['loaned_at']))
                    </div>
                    $ wlsize = book.get_waitinglist_size()
                    $if wlsize:
                        <div class="smallest lightgreen sansserif waitrank">
                            $ungettext("There is one person waiting for this book.", "There are %(n)d people waiting for this book.", wlsize, n=wlsize)
                        </div>
                </td>
                <td class="expires">
                    $if loan['expiry'] is None:
                        $# Not yet fulfilled
                        <span class="red">$_("Not yet downloaded.") </span><br/>
                        <a href="$loan['loan_link']">$_("Download Now")</a>
                    $else:
                        $:macros.FormatExpiry(loan['expiry'])
                </td>
                <td class="actions">
                    $if loan['resource_type'] == 'bookreader':
                        $:macros.LoanReadForm(loan['ocaid'])
                        $:macros.ReturnForm(loan['ocaid'])
                    $else:
                        <span class="smaller grey sansserif">$:_("Return via<br/>Adobe Digital Editions")</span>
                </td>
            </tr>
        </tbody>
    </table>
  </div>
  <!--
    $for loan in loans:
        $:macros.BorrowTrouble(loan['expiry'])
    <h3>$_("How To Return eBooks through Adobe Digital Editions")</h3>
    <ol class="sansserif small">
        <li>$_('Open Adobe Digital Editions')</li>
        <li>$_('Click the Library icon at the upper left corner')</li>
        <li>$_('Move your mouse over the cover of the eBook you wish to return')</li>
        <li>$_('Find the arrow at the upper left of the cover and open the eBook menu')</li>
        <li>$_('Click "Return Borrowed Item"')</li>
        <li>$_('Click "Return" to confirm')</li>
        <li>$_('Done!')</li>
    </ol>
    -->


<h1>$_("Books You're Waiting For")</h1>
$ waitinglist = user.get_waitinglist()
$if len(waitinglist) == 0:
    <div><em>$_('You are not waiting for any books at this moment.')</em></div>
$else:
    <div id="leave-waitinglist-dialog" class="hidden dialog"
         title="$_('Leave the Waiting List')">$:_('Are you sure you want to leave the waiting list of<br/><strong>TITLE</strong>?')</div>
    <script type="text/javascript">
    window.q.push(function() {
        \$("a.leave").click(function() {
            var title = \$(this).parents("tr").find(".book").text();
            \$("#leave-waitinglist-dialog strong").text(title);
            \$("#leave-waitinglist-dialog")
                .data("origin", \$(this))
                .dialog("open");
        });
    });
    </script>
    <div id="borrowTable" class="borrow collapse waitinglist">
    <table>
        <thead>
            <tr>
                <th class="titles" colspan="2">
                $ungettext("1 book", "%(count)d books", len(waitinglist), count=len(waitinglist))
                </th>
                <th class="status">$_("Status")</th>
                <th class="actions">$_("Actions")</th>
            </tr>
        </thead>
        <tbody>
        <!-- FIXME: Not internationalized. Uses internal status string directly -->
        $for record in waitinglist:
          $ book = record.get_book()
          $ status = record['status']
          $if book:
            <tr class="row-$status">
                <td class="cover">
                $:render_template('covers/book_cover_small', book)
                </td>
                <td class="titles">
                    <span class="book">
                        <a href="$book.key" class="borrowResults"><strong>$book.title</strong></a>
                    </span>
                    <span class="author">
                        $:macros.AuthorList(book.get_authors())
                    </span>
                    <div class="date">
                        $ ndays = record.get_waiting_in_days()
                        $ungettext("Waiting for 1 day", "Waiting for %(count)d days", ndays, count=ndays)
                    </div>
                    <div class="smallest lightgreen sansserif waitrank">
                        $if status == "waiting":
                            $ wsize = book.get_waitinglist_size()
                            $ pos = record.get_position()
                            $if pos == 1:
                                $_("You are the next person to receive this book.")
                            $else:
                                $ungettext("There is one person ahead of you in the waiting list.", "There are %(count)d people ahead of you in the waiting list.", pos-1, count=pos-1)
                    </div>
                </td>
                <td class="status">
                    <span class="status-value">$record['status'].title()</span><br/>
                    $if status == "available":
                        $ delta_hours = record.get_expiry_in_hours()
                        <div class="smaller" style="padding-top: 5px;">
                            $if delta_hours == 0:
                                $_("You have less than an hour to borrow it.")
                            $else:
                                $ungettext("You have one more hour to borrow it.", "You have %(hours)d more hours to borrow it.", delta_hours, hours=delta_hours)
                        </div>
                </td>
                <td class="actions">
                    $if status == "available":
                        <form action="$book.url('/borrow')" method="post">
                            <input type="hidden" name="action" value="borrow" />
                            <input type="submit" value=$_("Borrow Now") />
                        </form>
                    <form action="$book.url('/borrow?redirect=/account/loans')"
                          method="POST" class="wlform">
                        <input type="hidden" name="action" value="leave-waitinglist"/>
                        <a href="javascript:;" class="leave">$_("Leave the waiting list?")</a>
                    </form>
                </td>
            </tr>
        </tbody>
    </table>
    </div>


$# XXX borrow history not yet recorded so we show instructions if no current loans
<!-- if no borrow history -->
$if len(loans) == 0 and len(waitinglist) == 0:
    <div class="contentHalf">
        <p>
        $_("There are 2 different sources for borrowing books through Open Library:")</p>
        <p style="line-height:1em;margin:1.5em 0 1em;">
            <strong>
            <span style="font-size:1.875em;">1.</span>
            <span style="font-size:1.125em;">$_("Internet Archive"),</span>
            </strong>
            <span class="larger"><em>$_("eBooks everywhere")</em></span>
        </p>
        <p class="large">$:_('The <a href="//archive.org/">Internet Archive</a> and several partner libraries are offering thousands of eBooks for loan to anyone with an Open Library account, anywhere in the world.')</p>
        <p style="line-height:1em;margin:1.5em 0 1em;">
            <strong>
            <span style="font-size:1.875em;">2.</span>
            <span style="font-size:1.125em;">WorldCat,</span>
            </strong>
            <span class="larger"><em>$_("physical books from your local library")</em></span>
        </p>
        <p class="large">$_("We connect our records to WorldCat wherever possible. You can tell WorldCat your postcode/zip code, and it will tell you the closest library with a copy of the book.")</p>
    </div>
    <div class="contentSpacer"></div>
    <div class="contentQuarter">
        <div class="section">
            <h3>$_("Getting Started")</h3>
            <p class="small sansserif">$_("All you need to borrow a book scanned at the Internet Archive is an Open Library account and Adobe Digital Editions.")</p>
            <p class="small sansserif">$_("Loans through WorldCat are managed through your local libraries, not through Open Library.")</p>
            <!-- TODO: Language specific help page link? -->
            <p class="small sansserif">$:_('Check out the <a href="/help/faq#section-borrowing"><strong>Lending FAQ</strong></a> for more information.')</p>
        </div>
        <div class="section">
            <h3>$_("Which books are available?")</h3>
            <p class="small sansserif">$:_('Browse all the available books through the <a href="/subjects/lending_library">"Lending Library"</a> subject, or try a <a href="/search?subject_facet=Lending%20library">search</a>.')</p>
        </div>
    </div>
    <div class="contentSpacer"></div>
    <div class="contentQuarter">
        <h3>$_("Help/Downloads")</h3>
        <img src="/images/logo_adobeACS.png" alt="logo: Adobe" width="43" height="72" class="left" style="margin-right:10px;"/>
        <p class="small sansserif collapse"><a href="http://www.adobe.com/products/digitaleditions/#fp">$_("Download Adobe Digital Editions")</a></p>
        <p class="small sansserif">$:_('<a href="http://www.adobe.com/products/digitaleditions/help/">Help</a> on adobe.com')</p>
    </div>
    <div class="clearfix"></div>
    <p class="small sansserif lightgreen">$:_('If you work at a library and are interested to find out more about lending ebooks through Open Library, please <a href="/contact">get in touch with us</a>!')</p>

</div>
